package com.erp.erp_servers.log;


import com.erp.erp_entitys.LogDB.LogDBEntity;
import com.erp.utils.OSUtils;
import com.erp.utils.PropertiesUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * 日志写入数据库
 */
public class LogWriteDB {
    public List<LogDBEntity> readFileContent(String fileName) {
        List<LogDBEntity> list=new ArrayList<LogDBEntity>();
        //File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            Properties filelog = PropertiesUtils.loadProperties("filelog");
            String path="";
            //判断是什么操作系统
            if(OSUtils.isOSLinux()){
                path=filelog.getProperty("OSLinux");
            }
            if(OSUtils.isOSWin()){
                path=filelog.getProperty("OSWin");
            }

           // File file=new File(path+fileName);
            path=path+fileName;

            reader=new BufferedReader(new InputStreamReader(new FileInputStream(path),"GBK"));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                System.out.println(tempStr);
                list.add(conver(tempStr));
                sbf.append(tempStr);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                    return list;
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
       // return sbf.toString();
        return list;
    }
    private LogDBEntity conver(String str){
       String[] strs=str.split("->");
        LogDBEntity log=new LogDBEntity();
        log.setInTime(strs[0]);
        log.setUserName(strs[1]);
        log.setType(strs[2]);
        log.setTableName(strs[3]);
        log.setSql(strs[4]);
        //String[] params=strs[5].split(",");
        log.setParams(strs[5]);
        return log;
    }

    public static void main(String[] args) {
        LogWriteDB db=new LogWriteDB();
        String filename="d:/2021-07-15_log.txt";
        db.readFileContent(filename);
    }
}
